package com.ymttest.database.sqlwapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymt.core.tool.Logger;
import com.ymt.database.SqlManager;
import com.ymttest.database.model.YmtUserValidate;
import com.ymttest.utils.EnvSetup;

/************************************************************************************
 * @File name : YmtUserValidateWapper.java
 * @Author : zhouyi
 * @Date : 2015年3月30日
 * @Copyright : 洋码头
 ************************************************************************************/
public class YmtUserValidateWapper {
    SqlManager manage;

    public YmtUserValidateWapper() {
        manage = new SqlManager("ymt" + EnvSetup.getEnv());
    }

    /**
     * 获取对应手机号的最新一条验证码
     * 
     * @param phoneNum 手机号
     * @return 最新一条验证码,如果没有返回null
     * @throws InterruptedException
     */
    public String getValidateCode(String phoneNum) throws InterruptedException {
        String vcode = null;
        Map<String, String> map = new HashMap<String, String>();
        map.put("sactionid", phoneNum);
        Thread.sleep(1000);
        List<YmtUserValidate> userList = manage.getSelectResults(
                "com.ymttest.database.mapping.YmtUserValidateMapper.selectBySActionId", map);
        if (userList.size() > 0) {
            vcode = userList.get(0).getSvalidatecode();
        }
        // manage.close();
        return vcode;
    }


    /**
     * 关闭数据库连接
     */
    public void close() {
        manage.close();
    }

    @Override
    protected void finalize() throws Throwable {
        this.close();
        manage = null;
        super.finalize();
    }

    public static void main(String args[]) {
        try {
            Logger.debug(new YmtUserValidateWapper().getValidateCode("13588888889"));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
